package com.gjy.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-11-20 20:09:28
 */
public class ExcelUtil {

    public static List<Map<String, Object>> readExcel(String filename) throws IOException {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        SXSSFWorkbook workbook = new SXSSFWorkbook(
                new XSSFWorkbook(Files.newInputStream(Paths.get(filename)))
        );
        return list;
    }

    /**
     * 写
     *
     * @param os   文件名
     * @param list 文件内容
     */
    public static void writeExcel(ExcelConfig.Type type, String sheetName,
                                  OutputStream os, List<List<Object>> list) {

        Workbook workbook = null;
        try {
            switch (type) {
                case HSSF:
                    workbook = new HSSFWorkbook();
                    break;
                case XSSF:
                    workbook = new XSSFWorkbook();
                    break;
                case SXSSF:
                    workbook = new SXSSFWorkbook();
                    break;
                default:
                    throw new IllegalArgumentException("暂不支持 " + type + " 该类型");
            }

            Sheet sheet;
            if (StringUtils.hasText(sheetName)) {
                sheet = workbook.createSheet(sheetName);
            } else {
                sheet = workbook.createSheet();
            }
            for (int i = 0; i < list.size(); i++) {
                Row row = sheet.createRow(i);
                List<Object> rowV = list.get(i);
                int size = rowV.size();
                for (int j = 0; j < size; j++) {
                    Object v = rowV.get(j);
                    if (v instanceof Boolean) {
                        row.createCell(j, CellType.BOOLEAN).setCellValue(((Boolean) v));
                    } else if (v instanceof String) {
                        row.createCell(j, CellType.STRING).setCellValue(((String) v));
                    } else if (v instanceof Number) {
                        row.createCell(j, CellType.NUMERIC).setCellValue(((double) v));
                    } else if (v instanceof LocalDateTime) {
                        row.createCell(j, CellType.STRING);
                    }
                }
            }


            workbook.write(os);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (Objects.nonNull(workbook)) {
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static <T> void writeExcel(String sheetName, OutputStream os, List<T> list, Class<T> cls) {

    }
}
